package sanphone.com.runningschool;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import sanphone.com.runningschool.LogIn.LogInActivity;

public class BaseActivity extends AppCompatActivity {
    private ForceOfflineReceiver receiver;
    SharedPreferences sprfLogIn;
    SharedPreferences.Editor editorLogIn,editorMain;
    private NetWorkChangeReceiver netWorkChangeReceiver;
    private IntentFilter intentFilter1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e( "onCreate: ","start" );
        ActivityCollector.addActivity(this);
        intentFilter1 = new IntentFilter();
        intentFilter1.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangeReceiver=new NetWorkChangeReceiver();
        registerReceiver(netWorkChangeReceiver, intentFilter1);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("Intent.FORCE_OFFLINE");
        receiver=new ForceOfflineReceiver();
        registerReceiver(receiver,intentFilter);
        registerReceiver(netWorkChangeReceiver, intentFilter1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
        unregisterReceiver(netWorkChangeReceiver);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(receiver!=null){
            unregisterReceiver(receiver);
            receiver=null;
        }
    }

    private class ForceOfflineReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(final Context context, Intent intent) {
            AlertDialog.Builder builder=new AlertDialog.Builder(context);
            builder.setTitle("警告");
            builder.setMessage("您的账号由于发布某些信息已经被封闭!");
            builder.setCancelable(false);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    resetSprfMain();
                    startActivity(new Intent(context, LogInActivity.class));//跳到登录界面
                }
            });
            builder.show();
            builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    ActivityCollector.finishAll();//销毁所有的活动
                }
            });

        }
    }
    private class NetWorkChangeReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
            if(networkInfo!=null&&!networkInfo.isAvailable()){
                Toast.makeText(context,"当前网络不可用",Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void resetSprfMain(){
        sprfLogIn= PreferenceManager.getDefaultSharedPreferences(this);
        editorLogIn=sprfLogIn.edit();
        editorLogIn.putBoolean("main",false);
        editorLogIn.commit();
        editorMain= getSharedPreferences("data",MODE_PRIVATE).edit();
        editorMain.putString("schoolId",null);
        editorMain.putString("userName",null);
        editorMain.putString("userPhone",null);
        editorMain.putString("nickName",null);
        editorMain.putString("dormitory",null);
        editorMain.apply();
    }

}
